package com.liuwei.msmall.order.web;

import com.liuwei.msmall.order.service.OrderService;
import com.liuwei.msmall.order.vo.OrderConfirmVo;
import com.liuwei.msmall.order.vo.OrderListVo;
import com.liuwei.msmall.order.vo.OrderSubmitRespVo;
import com.liuwei.msmall.order.vo.OrderSubmitVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import java.util.List;
import java.util.concurrent.ExecutionException;

/**
 * @Classname OrderWebController
 * @Date 2021/4/30 19:37
 * @Created by 刘
 */
@Slf4j
@Controller
public class OrderWebController {

    @Autowired
    OrderService orderService;


    //去订单确认页
    @GetMapping("/totrade")
    public String toTrade(Model model) throws ExecutionException, InterruptedException {

        OrderConfirmVo confirmData= orderService.getOrderConfirmData();
        model.addAttribute("confirmData",confirmData);

        return "confirm";
    }


    //提交订单数据
    @PostMapping("/orderSubmit")
    public String orderSubmit(OrderSubmitVo vo, RedirectAttributes redirectAttributes){

        try {
            OrderSubmitRespVo respVo = orderService.submitOrder(vo);

            if (respVo.getCode()==0L){
                //表示提交订单成功,跳转订单列表
                return "redirect:http://order.msmall.com/orderlist.html";
            }
            String msg="创建订单失败：";
            switch (respVo.getCode().intValue()){
                case 1 : msg+="提交信息已过期，请刷新页面重新提交";break;
                case 2 : msg+="商品价格发生变化，请重新确认商品信息";break;
                case 3 : msg+="锁定库存失败";break;
            }
            redirectAttributes.addFlashAttribute("msg",msg);
            //提交失败，重新返回到订单确认页
            return "redirect:http://order.msmall.com/totrade";
        }catch (Exception e){
            log.error("异常：{}",e.getMessage());
            redirectAttributes.addFlashAttribute("msg","创建订单失败：出现异常");
            return "redirect:http://order.msmall.com/totrade";
        }
    }

    /**
     * 去个人订单列表
     * @param model
     * @return
     */
    @GetMapping("/orderlist.html")
    public String getOrderList(@RequestParam(required = false,defaultValue = "-1") Integer status, Model model){

        List<OrderListVo> listVos = orderService.getOrderList(status);
        model.addAttribute("orderList",listVos);
        model.addAttribute("status",status);

        return "orderList";
    }


    @GetMapping("/orderlist.html/{orderSn}")
    public String deleteOrder(@PathVariable("orderSn") String orderSn){

        orderService.deleteByOrderSn(orderSn);

        return "redirect:http://order.msmall.com/orderlist.html";
    }



}
